package opdracht4;

public class WeightedSearchTree<K extends Comparable<K>, V> extends
		SearchTree<K, V> 
{
	public WeightedSearchTree<K, V> createTree() 
	{
		return new WeightedSearchTree<K, V>();
	}
	public void balance() 
	{
		if (isEmpty()){return;}
		if (left.size() - right.size() > 1) 
		{
			SearchTree<K, V> biggest = left.getBiggest();
			right.put(this.key, this.value);
			value = biggest.value;
			key = biggest.key;
			left.remove(biggest.key);
		}
		else if (right.size() - left.size() > 1) 
		{
			SearchTree<K, V> smallest = right.getSmallest();
			left.put(this.key, this.value);
			value = smallest.value;
			key = smallest.key;
			right.remove(smallest.key);
		}
	}
	@Override
	public void put(K aKey, V aValue)
	{
		super.put(aKey, aValue);
		balance();
	}
	@Override
	public void remove(K aKey)
	{
		super.remove(aKey);
		balance();
	}
}
